package com.zhs.gccs.dao;

import com.zhs.gccs.model.PointInfo;
import com.orhanobut.logger.Logger;

import org.xutils.DbManager;
import org.xutils.db.sqlite.WhereBuilder;
import org.xutils.ex.DbException;

import java.util.List;

public class PointInfoDao {

    private final DbManager dbManager;


    public PointInfoDao() {
        this.dbManager = XUtilsManager.getInstance().getDbManager();
    }

    public void savePointInfo(PointInfo pointInfo){
        PointInfo pointInfo1 = null;
        try {
            WhereBuilder whereBuilder = WhereBuilder.b("pointId", "=", pointInfo.getPointId());
            pointInfo1 = dbManager.selector(PointInfo.class).where(whereBuilder).findFirst();
            if(pointInfo1 == null){
                Logger.i("不存在核查项 "+pointInfo.getPointId()+",保存到库中");
                dbManager.save(pointInfo);
            }else {
                Logger.i("存在核查项 "+pointInfo.getPointId()+",更新数据");
                pointInfo.setId(pointInfo1.getId());
                dbManager.update(pointInfo, "isCheck","readme", "isExist", "imagePath1", "imagePath2", "imagePath3");
            }
        } catch (DbException e) {
            e.printStackTrace();
        }
    }

    public void addPointInfo(List<PointInfo> list){
        try {
            List<PointInfo> pointInfoList0 = dbManager.selector(PointInfo.class).findAll();
            if(pointInfoList0 == null){
                dbManager.save(list);
            }else {
//                for (PointInfo pointInfo0 : pointInfoList0){
//                    for (PointInfo pointInfo : list){
//                        if(!pointInfo0.getPointId().equals(pointInfo.getPointId())){
//                            dbManager.save(pointInfo);
//                        }
//                    }
//                }
            }
        } catch (DbException e) {
            e.printStackTrace();
        }
    }

    public void addPointInfo(String pid,List<PointInfo> list){
        try {
            WhereBuilder whereBuilder = WhereBuilder.b("pId", "=",pid);
            PointInfo pointInfo0 = dbManager.selector(PointInfo.class).where(whereBuilder).findFirst();
            if(pointInfo0 == null){
                dbManager.save(list);
            }else {

            }
        } catch (DbException e) {
            e.printStackTrace();
        }
    }

    public PointInfo findPointInfo(String bsm){
        PointInfo pointInfo = null;

        try {
            WhereBuilder whereBuilder = WhereBuilder.b("pointId", "=", bsm);
            pointInfo = dbManager.selector(PointInfo.class).where(whereBuilder).findFirst();
        } catch (DbException e) {
            e.printStackTrace();
        }

        return pointInfo;
    }

    public List<PointInfo> findPointInfoListAll(String pId){
        List<PointInfo> list = null;
        try {
            WhereBuilder whereBuilder = WhereBuilder.b("pId", "=", pId);
            list = dbManager
                    .selector(PointInfo.class).where(whereBuilder)
                    .findAll();
        } catch (DbException e) {
            e.printStackTrace();
        }
        return list;
    }
}
